/**
 * Created by huangying on 16/11/11.
 */
function edit_article() {
    $.ajax({
        type: 'get',
        url: url_link + 'articles/' + update_id,
        async: false,
        data: data_motocycle,
        success: function (data) {
            try {
                var obj = JSON.parse(data);
                var edit_article_list = "";
                var edit_article_thumb_list = "";
                var interest_list_content = "";
                if (obj.code == 0) {
                    //===============================================标题===============================================//
                    $('.left .thread-title').val(obj.data.title);
                    var _length = 29 - obj.data.title.length;

                    $(".title-box").find('.char-limit em').text('还可以输入');
                    $(".title-box").find('.char-limit i').text(_length);
                    $(".title-box").find('.char-limit i').css('color', '#2175d0');
                    $(".title-box").find('.char-limit b').text('字');

                    //===============================================类型===============================================//
                    if (obj.data.type == 0) {
                        //原创
                        $('.by_myself').addClass('resourceChoice-curr');
                    } else {
                        //转载
                        $('.by_other').addClass('resourceChoice-curr');
                    }

                    //===============================================车型===============================================//
                    interest_list_content = "<li id='" + obj.data.mototype_id + "'>" +
                        "<a>" +
                        "<span>" + obj.data.mototype_name + "</span>" +
                        "</a>" +
                        "<a href='javascript:;'>" +
                        "</a>" +
                        "<a href='javascript:;' class='remove'>×</a>" +
                        "</li>";


                    //===============================================封面图===============================================//
                    if (obj.data.cover) {
                        edit_article_list += '<li id="mainSort1" data-name="1" data-sort="1" type="image" >' +
                            '<div class="mainSortBox">' +
                            '<a href="javascript:;" class="remove thread-model-remove">×</a>' +
                            '<h3 class="sort-handle">' +
                            '<span><i></i><i></i><i></i></span>' +
                            '</h3>' +
                            '<div class="content-box" style="overflow:hidden;">' +
                            '<img src=' + obj.data.cover + ' style="height: 100%;" class="cover"></div>' +
                            '<button class="button_cover" data-cover="true" style="opacity: 0">设为封面</button>' +
                            '<textarea placeholder="在此添加上方图片的描述，描述不超过500字。" class="description-input"></textarea>' +
                            '</div>' +
                            '<a href="javascript:;" class="insert-model-btn" title="插入模块">+</a>' +
                            '<span class="add-model" style="height:0;margin-top:0;margin-bottom:10px">' +
                            '<a href="javascript:;" class="close-add-btn-box">×</a>' +
                            '<div class="add-model-box">' +
                            '<div class="intro">' +
                            '<h5>点击右侧按钮，添加相对应的元素模块</h5>' +
                            '<h6>还不太会用？看看<a href="publish-course.html" target="_blank">发帖教程</a></h6>' +
                            '</div>' +
                            '<div class="btn-box">' +
                            '<a href="javascript:;" class="add-txt-btn" id="add-txt1"><i></i>添加文字</a>' +
                            '<a href="javascript:;" class="add-img-btn" id="add-img1"><i></i>添加图片</a>' +
                            '<a href="javascript:;" class="add-video-btn" id="add-video1"><i></i>添加视频</a>' +
                            '</div>' +
                            '</div>' +
                            '</span>' +
                            '</li>';
                        //右侧小图
                        edit_article_thumb_list += '<li id="thumb1" type="image" data-name="1">' +
                            '<a href="javascript:;">' +
                            '<em class="emReorder">1</em>' +
                            '<img src=' + obj.data.cover + '  style="width: 124px;height: 82px;">' +
                            '</a>' +
                            '</li>';
                    }


                    //===============================================内容===============================================//
                    var _content = obj.data.content.split("$$##MotorTravel##$$");
                    var _i = 0;
                    $.each(_content, function (i, item) {
                        if (obj.data.cover) {
                            _i = i + 2
                        } else {
                            _i = i + 1
                        }
                        var n = item.substring(0, 12);
                        var m = item.substring(0, 13);
                        if (n == "$$##Text##$$") {
                            item = item.substring(12, item.length);
                            edit_article_list += '<li id="mainSort' + _i + '" data-name="' + _i + '" data-sort="' + _i + '" type="text">' +
                                '<div class="mainSortBox">' +
                                '<a href="javascript:;" class="remove thread-model-remove">×</a>' +
                                '<h3 class="sort-handle">' +
                                '<span><i></i><i></i><i></i></span>' +
                                '</h3>' +
                                '<textarea placeholder="在此输入正文内容。" class="txt-input" style="color:black;" >' + item + '</textarea>' +
                                '</div>' +
                                '<a href="javascript:;" class="insert-model-btn" title="插入模块">+</a>' +
                                '<span class="add-model" style="height:0;margin-top:0;margin-bottom:10px">' +
                                '<a href="javascript:;" class="close-add-btn-box">×</a>' +
                                '<div class="add-model-box">' +
                                '<div class="intro">' +
                                '<h5>点击右侧按钮，添加相对应的元素模块</h5>' +
                                '<h6>还不太会用？看看<a href="publish-course.html" target="_blank">发帖教程</a></h6>' +
                                '</div>' +
                                '<div class="btn-box">' +
                                '<a href="javascript:;" class="add-txt-btn" id="add-txt' + _i + '"><i></i>添加文字</a>' +
                                '<a href="javascript:;" class="add-img-btn" id="add-img' + _i + '"><i></i>添加图片</a>' +
                                '<a href="javascript:;" class="add-video-btn" id="add-video' + _i + '"><i></i>添加视频</a>' +
                                '</div>' +
                                '</div>' +
                                '</span>' +
                                '</li>';
                            edit_article_thumb_list += '<li id="thumb' + _i + '" data-name="' + _i + '" type="text" >' +
                                '<a href="javascript:;">' +
                                '<em class="emReorder">' + _i + '</em>' +
                                '<img src="../../image/publish_txt.png"></a></li>';
                        } else if (m == "$$##Image##$$") {
                            item = item.substring(13, item.length);
                            edit_article_list += '<li id="mainSort' + _i + '" data-name="' + _i + '" data-sort="' + _i + '" type="image" >' +
                                '<div class="mainSortBox">' +
                                '<a href="javascript:;" class="remove thread-model-remove">×</a>' +
                                '<h3 class="sort-handle">' +
                                '<span><i></i><i></i><i></i></span>' +
                                '</h3>';

                            var _content_image_description = item.split("$$##ImageDescription##$$");
                            var _content_image_url = _content_image_description[0].split("$$##ImageInfo##$$");
                            var _location_begin = _content_image_url[1].indexOf('AspectRatioBegin')//判断AspectRatioBegin出现的位置,然后便于截取数据
                            var _location_end = _content_image_url[1].indexOf('AspectRatioEnd')//判断AspectRatioEnd出现的位置,然后便于截取数据

                            var _img_width = _content_image_url[1].substring(5, _location_begin);
                            var ratio = _content_image_url[1].substring(_location_begin + 16, _location_end);
                            if (_img_width > 728) {
                                var addImgHtml_show_height = 728 / ratio
                            } else {
                                var addImgHtml_show_height = _img_width / ratio
                            }

                            edit_article_list += '<div class="content-box" style="overflow:hidden;height: ' + addImgHtml_show_height + 'px;">' +
                                '<img src=' + _content_image_url[0] + ' style="height: 100%;" data-width=' + _img_width + ' data-ratio=' + ratio + '></div>';

                            if (obj.data.cover == _content_image_url[0]) {
                                edit_article_list += '<button class="button_cover" data-cover="true" style="opacity: 0">设为封面</button>'
                            } else {
                                edit_article_list += '<button class="button_cover" data-cover="false" >设为封面</button>'
                            }

                            if (_content_image_description.length == 1) {
                                edit_article_list += '<textarea placeholder="在此添加上方图片的描述，描述不超过500字。" class="description-input"></textarea>'
                            } else if (_content_image_description.length == 2) {
                                edit_article_list += '<textarea  class="description-input">' + _content_image_description[1] + '</textarea>'
                            }

                            edit_article_list += '</div>' +
                                '<a href="javascript:;" class="insert-model-btn" title="插入模块">+</a>' +
                                '<span class="add-model" style="height:0;margin-top:0;margin-bottom:10px">' +
                                '<a href="javascript:;" class="close-add-btn-box">×</a>' +
                                '<div class="add-model-box">' +
                                '<div class="intro">' +
                                '<h5>点击右侧按钮，添加相对应的元素模块</h5>' +
                                '<h6>还不太会用？看看<a href="publish-course.html" target="_blank">发帖教程</a></h6>' +
                                '</div>' +
                                '<div class="btn-box">' +
                                '<a href="javascript:;" class="add-txt-btn" id="add-txt' + _i + '"><i></i>添加文字</a>' +
                                '<a href="javascript:;" class="add-img-btn" id="add-img' + _i + '"><i></i>添加图片</a>' +
                                '<a href="javascript:;" class="add-video-btn" id="add-video' + _i + '"><i></i>添加视频</a>' +
                                '</div>' +
                                '</div>' +
                                '</span>' +
                                '</li>';
                            //右侧小图
                            edit_article_thumb_list += '<li id="thumb' + _i + '"  data-name="' + _i + '" type="image">' +
                                '<a href="javascript:;">' +
                                '<em class="emReorder">' + _i + '</em>' +
                                '<img src=' + _content_image_url[0] + '  style="width: 124px;height: 82px;"></a></li>';

                        } else if (m == "$$##Video##$$") {
                            item = item.substring(13, item.length);
                            edit_article_list += '<li id="mainSort' + _i + '" data-name="' + _i + '" data-sort="' + _i + '" type="video" >' +
                                '<div class="mainSortBox">' +
                                '<a href="javascript:;" class="remove thread-model-remove">×</a>' +
                                '<h3 class="sort-handle">' +
                                '<span><i></i><i></i><i></i></span>' +
                                '</h3>';

                            var _content_video_description = item.split("$$##VideoDescription##$$");
                            edit_article_list += '<div class="content-box" style="overflow:hidden">' +
                                '<video src=' + _content_video_description[0] + ' style="width: 728px;" controls="controls"></div>';
                            if (_content_video_description.length == 1) {
                                edit_article_list += '<textarea placeholder="在此添加上方视频的描述，描述不超过500字。" class="description-input"></textarea>'
                            } else if (_content_video_description.length == 2) {
                                edit_article_list += '<textarea class="description-input">' + _content_video_description[1] + '</textarea>'
                            }


                            edit_article_list += '</div>' +
                                '<a href="javascript:;" class="insert-model-btn" title="插入模块">+</a>' +
                                '<span class="add-model" style="height:0;margin-top:0;margin-bottom:10px">' +
                                '<a href="javascript:;" class="close-add-btn-box">×</a>' +
                                '<div class="add-model-box">' +
                                '<div class="intro">' +
                                '<h5>点击右侧按钮，添加相对应的元素模块</h5>' +
                                '<h6>还不太会用？看看<a href="publish-course.html" target="_blank">发帖教程</a></h6>' +
                                '</div>' +
                                '<div class="btn-box">' +
                                '<a href="javascript:;" class="add-txt-btn" id="add-txt' + _i + '"><i></i>添加文字</a>' +
                                '<a href="javascript:;" class="add-img-btn" id="add-img' + _i + '"><i></i>添加图片</a>' +
                                '<a href="javascript:;" class="add-video-btn" id="add-video' + _i + '"><i></i>添加视频</a>' +
                                '</div>' +
                                '</div>' +
                                '</span>' +
                                '</li>';
                            //右侧小图
                            edit_article_thumb_list += '<li id="thumb' + _i + '" data-name="' + _i + '" type="video" >' +
                                '<a href="javascript:;">' +
                                '<em class="emReorder">' + _i + '</em>' +
                                '<video src=' + _content_video_description[0] + '  style="width: 124px;height: 82px;"></a></li>';
                        }
                    });
                    $('.ui-sortable').append(edit_article_list);
                    $(".thumbnail-box ol").append(edit_article_thumb_list);
                    $('.interest-label-list').html(interest_list_content);
                    $('.add-interest-btn').hide();

                    //封面图宽高处理
                    var img = new Image();
                    img.src = $("#mainSort1 img").attr("src");
                    var _width = 0;
                    var _height = 0;
                    var _ratio = 0;
                    // 完全加载完毕的事件
                    img.onload = function () {
                        _width = img.width;
                        _height = img.height;
                        _ratio = img.width / img.height;

                        if (_width > 728) {
                            var addImgHtml_show_height = 728 / _ratio
                        } else {
                            var addImgHtml_show_height = _height
                        }
                        $("#mainSort1 .content-box").css('height', addImgHtml_show_height + 'px');

                    }


                }
            } catch (e) {

            }
        }
    })
}


//编辑状态下的保存按钮(草稿箱)
$(document).on('click', '#save-update-btn', function () {
    info_get();
    if (!title_info) {
        layer.msg('未输入标题!')
    } else if (title_info.length < 6 || title_info.length > 30) {
        layer.msg('标题字数长度出错!')
    } else if (!motorcycle_info) {
        layer.msg('未选择类别!')
    } else if (!content_info) {
        layer.msg('未填入任何内容!')
    } else if (!content_cover) {
        layer.msg('您还未设置封面,是否继续', {
            time: 0, //不自动关闭
            btn: ['是', '否'],
            yes: function (index) {
                layer.close(index);
                if (update_status == 0) {
                    $('.loading').show();
                    setTimeout(function () {
                        //本来就是在草稿箱里面的,现在还去草稿箱
                        $.ajax({
                            type: 'put',
                            url: url_link + 'articles/' + update_id,
                            async: false,
                            data: {
                                user_id: user_id,
                                type: article_type,
                                title: title_info,
                                mototype_id: motorcycle_info,
                                content: content_info
                            },
                            success: function (data) {
                                var obj = JSON.parse(data);
                                if (obj.code == 0) {
                                    $('.loading').hide();
                                    layer.msg("保存成功,正在为您跳转页面...");
                                    setTimeout(function () {
                                        window.location.href = "../center/drafts.html";
                                    }, 1000)
                                }
                            }
                        });
                    },3000)

                } else {
                    $('.loading').show();
                    setTimeout(function () {
                        //原来是发表的文章,现在去草稿箱了(状态修改)
                        $.ajax({
                            type: 'post',
                            url: url_link + 'articles/drafts',
                            async: false,
                            data: {
                                user_id: user_id,
                                type: article_type,
                                title: title_info,
                                mototype_id: motorcycle_info,
                                content: content_info,
                                article_id: update_id
                            },
                            success: function (data) {
                                var obj = JSON.parse(data);
                                if (obj.code == 0) {
                                    $('.loading').hide();
                                    layer.msg("保存成功,正在为您跳转页面...");
                                    setTimeout(function () {
                                        window.location.href = "../center/drafts.html";
                                    }, 1000)
                                }
                            }
                        });
                    },3000)

                }

            }
        });
    } else {
        if (update_status == 0) {
            $('.loading').show();
            setTimeout(function () {
                //本来就是在草稿箱里面的,现在还去草稿箱
                $.ajax({
                    type: 'put',
                    url: url_link + 'articles/' + update_id,
                    async: false,
                    data: {
                        user_id: user_id,
                        type: article_type,
                        title: title_info,
                        mototype_id: motorcycle_info,
                        content: content_info,
                        cover: content_cover
                    },
                    success: function (data) {
                        var obj = JSON.parse(data);
                        if (obj.code == 0) {
                            $('.loading').hide();
                            layer.msg("保存成功,正在为您跳转页面...");
                            setTimeout(function () {
                                window.location.href = "../center/drafts.html";
                            }, 1000)
                        }
                    }
                });
            },3000)

        } else {
            $('.loading').show();
            setTimeout(function () {
                //原来是发表的文章,现在去草稿箱了(状态修改)
                $.ajax({
                    type: 'post',
                    url: url_link + 'articles/drafts',
                    async: false,
                    data: {
                        user_id: user_id,
                        type: article_type,
                        title: title_info,
                        mototype_id: motorcycle_info,
                        content: content_info,
                        article_id: update_id,
                        cover: content_cover
                    },
                    success: function (data) {
                        var obj = JSON.parse(data);
                        if (obj.code == 0) {
                            $('.loading').hide();
                            layer.msg("保存成功,正在为您跳转页面...");
                            setTimeout(function () {
                                window.location.href = "../center/drafts.html";
                            }, 1000)
                        }
                    }
                });
            },3000)

        }

    }
});


//编辑状态下的发布按钮
$(document).on('click', '#pub-update-btn', function () {
    info_get();
    if (!title_info) {
        layer.msg('未输入标题!')
    } else if (title_info.length < 6 || title_info.length > 30) {
        layer.msg('标题字数长度出错!')
    } else if (!motorcycle_info) {
        layer.msg('未选择类别!')
    } else if (!content_info) {
        layer.msg('未填入任何内容!')
    } else if (!content_cover) {
        layer.msg('您还未设置封面,是否继续', {
            time: 0, //不自动关闭
            btn: ['是', '否'],
            yes: function (index) {
                layer.close(index);
                if (update_status == 0) {
                    $('.loading').show();
                    setTimeout(function () {
                        //本来就是在草稿箱里面的,现在改成发布的文章(状态修改)
                        $.ajax({
                            type: 'post',
                            url: url_link + 'articles',
                            async: false,
                            data: {
                                user_id: user_id,
                                type: article_type,
                                title: title_info,
                                mototype_id: motorcycle_info,
                                content: content_info,
                                article_id: update_id
                            },
                            success: function (data) {
                                var obj = JSON.parse(data);
                                if (obj.code == 0) {
                                    $('.loading').hide();
                                    layer.msg("发表成功,正在为您跳转页面...");
                                    setTimeout(function () {
                                        window.location.href = "show.html?article_id=" + obj.data.article_id;
                                    }, 1000)
                                }
                            }
                        });
                    },3000)

                } else {
                    $('.loading').show();
                    setTimeout(function () {
                        //本来就是发布的文章,现在还是发布的文章
                        $.ajax({
                            type: 'put',
                            url: url_link + 'articles/' + update_id,
                            async: false,
                            data: {
                                user_id: user_id,
                                type: article_type,
                                title: title_info,
                                mototype_id: motorcycle_info,
                                content: content_info
                            },
                            success: function (data) {
                                var obj = JSON.parse(data);
                                if (obj.code == 0) {
                                    $('.loading').hide();
                                    layer.msg("发表成功,正在为您跳转页面...");
                                    setTimeout(function () {
                                        window.location.href = "show.html?article_id=" + obj.data.article_id;
                                    }, 1000)
                                }
                            }
                        });
                    },3000)

                }

            }
        });
    } else {
        if (update_status == 0) {
            $('.loading').show();
            setTimeout(function () {
                //本来就是在草稿箱里面的,现在改成发布的文章(状态修改)
                $.ajax({
                    type: 'post',
                    url: url_link + 'articles',
                    async: false,
                    data: {
                        user_id: user_id,
                        type: article_type,
                        title: title_info,
                        mototype_id: motorcycle_info,
                        content: content_info,
                        article_id: update_id,
                        cover: content_cover
                    },
                    success: function (data) {
                        var obj = JSON.parse(data);
                        if (obj.code == 0) {
                            $('.loading').hide();
                            layer.msg("发表成功,正在为您跳转页面...");
                            setTimeout(function () {
                                window.location.href = "show.html?article_id=" + obj.data.article_id;
                            }, 1000)
                        }
                    }
                });
            },3000)
        } else {
            $('.loading').show();
            setTimeout(function () {
                //本来就是发布的文章,现在还是发布的文章
                $.ajax({
                    type: 'put',
                    url: url_link + 'articles/' + update_id,
                    async: false,
                    data: {
                        user_id: user_id,
                        type: article_type,
                        title: title_info,
                        mototype_id: motorcycle_info,
                        content: content_info,
                        cover: content_cover
                    },
                    success: function (data) {
                        var obj = JSON.parse(data);
                        if (obj.code == 0) {
                            $('.loading').hide();
                            layer.msg("发表成功,正在为您跳转页面...");
                            setTimeout(function () {
                                window.location.href = "show.html?article_id=" + obj.data.article_id;
                            }, 1000)
                        }
                    }
                });
            },3000)

        }

    }
})

